-- Saved Variables
 FarmTracker_Save        = {};   
 
 local FarmTrackerFrame
 local closeButton
 local FarmTrackerSlotButton = {};
  
  
 function FarmTracker_out(text)
         DEFAULT_CHAT_FRAME:AddMessage(text)
 end
 
 function FarmTracker_ShowFrame()
         FarmTrackerFrame:Show();
 end
 
 function FarmTracker_HideFrame()
         FarmTrackerFrame:Hide()
 end
 
 function SetExtendedItemButtonCount(button, count)
         if ( not button ) then
                 return;
         end
  
         if ( not count ) then
                 count = 0;
         end
  
         button.count = count;
         if ( count >= 0 ) then
                 if ( count > 999 ) then
                         count = "*";
                 end
                 getglobal(button:GetName().."Count"):SetText(count);
                 getglobal(button:GetName().."Count"):Show();
         else
                 getglobal(button:GetName().."Count"):Hide();
         end
 end
 
 function FarmTracker_UpdateButtons()
         for buttonNum=1,12 do
                 if FarmTrackerSlotButton[buttonNum].itemLink then
                 local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture = GetItemInfo(FarmTrackerSlotButton[buttonNum].itemLink )
                 SetExtendedItemButtonCount(FarmTrackerSlotButton[buttonNum],GetItemCount(itemName))
                 FarmTrackerSlotButton[buttonNum]:SetScript("OnEnter", function(self)
                 GameTooltip:SetOwner(self, "ANCHOR_LEFT");
                 GameTooltip:SetText(itemLink);
                 GameTooltip:Show();
                 end);
                 FarmTrackerSlotButton[buttonNum]:SetScript("Onleave", function(self)
                 GameTooltip:Hide();
                 end);
                 end
         end
 end
 
 --[[
FarmTracker Main Frame
 --]]
 FarmTrackerFrame = CreateFrame("Frame", "FarmTrackerFrame", UIParent)
 FarmTrackerFrame:SetWidth(250)
 FarmTrackerFrame:SetHeight(180)
 FarmTrackerFrame:SetPoint("CENTER",UIParet)
 FarmTrackerFrame:SetMovable(1)
 FarmTrackerFrame:EnableMouse(1)
 FarmTrackerFrame:SetBackdrop({
         bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
         edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
         tile = true, edgeSize = 16, tileSize = 16,
         insets = {left = 5, right = 5, top = 5, bottom = 5},
 })
 FarmTrackerFrame:SetBackdropColor(24/255, 24/255, 24/255, 1)
 FarmTrackerFrame.onMouseDown = function()
         FarmTrackerFrame:StartMoving()
 end
 FarmTrackerFrame.onMouseUp = function()
         FarmTrackerFrame:StopMovingOrSizing()
 end
 FarmTrackerFrame:SetScript("OnMouseDown", FarmTrackerFrame.onMouseDown)
 FarmTrackerFrame:SetScript("OnMouseUp", FarmTrackerFrame.onMouseUp)
 FarmTrackerFrame:Hide()
 
 FarmTrackerFrame:RegisterEvent("BAG_UPDATE")
 FarmTrackerFrame:SetScript("OnEvent", FarmTracker_UpdateButtons)

 
 
 --[[
 Header Text on Main Frame
 --]]
 
 FarmTrackerFrameHeaderText = FarmTrackerFrame:CreateFontString(nil, "OVERLAY")
 FarmTrackerFrameHeaderText:SetPoint("CENTER", FarmTrackerFrame, "TOP", 0, -15)
 FarmTrackerFrameHeaderText:SetFontObject(GameFontNormalLarge)
 FarmTrackerFrameHeaderText:SetTextColor((128/255), (128/255), (255/255))
 FarmTrackerFrameHeaderText:SetText("FarmTracker")
 
 
 --[[
 Instructions on Main Frame
 --]]
 
 FarmTrackerFrameInstructionText = FarmTrackerFrame:CreateFontString(nil, "OVERLAY")
 FarmTrackerFrameInstructionText:SetPoint("TOPLEFT", FarmTrackerFrame, "TOPLEFT", 0, -30)
 FarmTrackerFrameInstructionText:SetWidth(260)
 FarmTrackerFrameInstructionText:SetJustifyH("CENTER")
 FarmTrackerFrameInstructionText:SetFontObject(GameFontNormal)
 FarmTrackerFrameInstructionText:SetTextColor(1,1,1)
 FarmTrackerFrameInstructionText:SetText("Drop items below to track them.")
 
 --[[
 Right Click Instructions on main Frame
 --]]
 
 FarmTrackerFrameRightClickText = FarmTrackerFrame:CreateFontString(nil, "OVERLAY")
 FarmTrackerFrameRightClickText:SetPoint("TOPLEFT", FarmTrackerFrame, "TOPLEFT", 0, -45)
 FarmTrackerFrameRightClickText:SetWidth(260)
 FarmTrackerFrameRightClickText:SetJustifyH("CENTER")
 FarmTrackerFrameRightClickText:SetFontObject(GameFontNormal)
 FarmTrackerFrameRightClickText:SetTextColor(1,0,0)
 FarmTrackerFrameRightClickText:SetText("(Right Click an item to remove it.)")
 
 --[[
 Close Button on Main Frame
 --]]
 optionsCloseButton = CreateFrame("Button", "FarmTracker_optionsCloseButton", FarmTrackerFrame, "UIPanelCloseButton");
 optionsCloseButton:SetPoint("TOPRIGHT", FarmTrackerFrame, "TOPRIGHT", 0, 0);
 optionsCloseButton:SetScript("OnClick", function(self)
         this:GetParent():Hide()
 end);
 
 --[[
 ActionButtons for the Main Frame
 --]]
         for buttonNum=1,12 do
                 FarmTrackerSlotButton[buttonNum] =  CreateFrame("Button", "FarmTrackerSlotButton"..buttonNum, FarmTrackerFrame, "ItemButtonTemplate");
                 FarmTrackerSlotButton[buttonNum]:RegisterForClicks("LeftButtonDown","RightButtonDown")
                 FarmTrackerSlotButton[buttonNum]:SetScript("OnReceiveDrag", function(self)
                         if CursorHasItem() then
                                 local objtype, _, link = GetCursorInfo()
                                 if objtype == "item" then
                                         local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture = GetItemInfo(link)
                                         SetItemButtonTexture(self, itemTexture)
                                         self.itemLink = itemLink
                                         FarmTracker_UpdateButtons()
                                         ClearCursor()
                                 end
                         end
                 end);
                 FarmTrackerSlotButton[buttonNum]:SetScript("OnClick", function(self)
                         if ( arg1 == "RightButton" ) then
                                         SetItemButtonTexture(self,nil)
                                         SetItemButtonCount(self,nil)
                                         self.itemLink = nil
                                         self:SetScript("OnEnter", nil)
                                         self:SetScript("OnLeave", nil)
                                         ClearCursor()
                         elseif CursorHasItem() then
                                 local objtype, _, link = GetCursorInfo()
                                 if objtype == "item" then
                                         local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture = GetItemInfo(link)
                                         SetItemButtonTexture(self, itemTexture)
                                         self.itemLink = itemLink
                                         FarmTracker_UpdateButtons()
                                         ClearCursor()
                                 end
                         end
                 end);
                 if (buttonNum == 1) then
                         FarmTrackerSlotButton[buttonNum]:SetPoint("TOPLEFT", FarmTrackerFrame, "TOPLEFT", 15, -65)
                 elseif (buttonNum == 7) then
                         FarmTrackerSlotButton[buttonNum]:SetPoint("TOPLEFT", FarmTrackerFrame, "TOPLEFT",  15, -115)
                 else
                         FarmTrackerSlotButton[buttonNum]:SetPoint("LEFT", "FarmTrackerSlotButton"..buttonNum - 1, "RIGHT", 0, 0)
                 end
 end
 
 SlashCmdList["FARMTRACKER"] = function(msg)
     if (msg == "r") then
                 ReloadUI();
         elseif (msg == "show") then
                 FarmTracker_ShowFrame()
         elseif (msg == "hide") then
                 FarmTracker_HideFrame()
         elseif (msg == "u") then
                 FarmTracker_UpdateButtons()
         else
                 FarmTracker_out("FarmTracker: /farmTracker [show,hide]")
         end
 end
 SLASH_FARMTRACKER1 = "/farmtracker"
 SLASH_FARMTRACKER2 = "/ft"
 
 function FarmTracker_MinimapHandler()
      if FarmTrackerFrame:IsVisible() then
                 FarmTrackerFrame:Hide()
         else
                 FarmTrackerFrame:Show()
         end
 end
 
 --[[
 
 --  Minimap Button -- 
 Thanks to Xinhuan for this code.
 
 --]]
 
 CreateFrame("Button", "FarmTracker_MinimapButton", Minimap);
 FarmTracker_MinimapButton:EnableMouse(true);
 FarmTracker_MinimapButton:SetMovable(false);
 FarmTracker_MinimapButton:SetFrameStrata("LOW");
 FarmTracker_MinimapButton:SetWidth(33);
 FarmTracker_MinimapButton:SetHeight(33);
 FarmTracker_MinimapButton:SetPoint("TOPLEFT", Minimap, "RIGHT", 2, 0);
 FarmTracker_MinimapButton:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight");
 
 FarmTracker_MinimapButton:CreateTexture("FarmTracker_MinimapButtonIcon", "BORDER");
 FarmTracker_MinimapButtonIcon:SetWidth(20);
 FarmTracker_MinimapButtonIcon:SetHeight(20);
 FarmTracker_MinimapButtonIcon:SetPoint("CENTER", -2, 1);
 FarmTracker_MinimapButtonIcon:SetTexture("Interface\\Icons\\INV_Misc_Bag_10_Blue");
 
 FarmTracker_MinimapButton:CreateTexture("FarmTracker_MinimapButtonBorder", "OVERLAY");
 FarmTracker_MinimapButtonBorder:SetWidth(52);
 FarmTracker_MinimapButtonBorder:SetHeight(52);
 FarmTracker_MinimapButtonBorder:SetPoint("TOPLEFT");
 FarmTracker_MinimapButtonBorder:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder");
 
 function FarmTracker_MinimapButton_Init()
         -- Initialise defaults if not present
         if (FarmTracker_Save.ButtonShown == false) then
                 FarmTracker_MinimapButton:Hide();
                 FarmTracker_Save.ButtonShown = false;
         else
                 FarmTracker_MinimapButton:Show();
                 FarmTracker_Save.ButtonShown = true;
         end
         FarmTracker_Save.ButtonRadius = FarmTracker_Save.ButtonRadius or 78;
         FarmTracker_Save.ButtonPosition = FarmTracker_Save.ButtonPosition or 345;
         FarmTracker_MinimapButton_UpdatePosition();
 end
 
 function FarmTracker_MinimapButton_UpdatePosition()
         FarmTracker_MinimapButton:SetPoint(
                 "TOPLEFT",
                 "Minimap",
                 "TOPLEFT",
                 54 - (FarmTracker_Save.ButtonRadius * cos(FarmTracker_Save.ButtonPosition)),
                 (FarmTracker_Save.ButtonRadius * sin(FarmTracker_Save.ButtonPosition)) - 55
         );
 end
 
 -- Thanks to Yatlas for this code
 function FarmTracker_MinimapButton_BeingDragged()
         -- Thanks to Gello for this code
         local xpos,ypos = GetCursorPosition();
         local xmin,ymin = Minimap:GetLeft(), Minimap:GetBottom();
 
         xpos = xmin-xpos/UIParent:GetScale()+70;
         ypos = ypos/UIParent:GetScale()-ymin-70;
 
         local v = math.deg(math.atan2(ypos, xpos));
         if (v < 0) then
                 v = v + 360;
         end
         FarmTracker_Save.ButtonPosition = v;
         FarmTracker_MinimapButton_UpdatePosition();
 
 end
 
 FarmTracker_MinimapButton:RegisterEvent("VARIABLES_LOADED");
 FarmTracker_MinimapButton:RegisterForDrag("RightButton");
 FarmTracker_MinimapButton:SetScript("OnDragStart", function(self)
         self:SetScript("OnUpdate", FarmTracker_MinimapButton_BeingDragged);
 end);
 FarmTracker_MinimapButton:SetScript("OnDragStop", function(self)
         self:SetScript("OnUpdate", nil);
 end);
 FarmTracker_MinimapButton:SetScript("OnClick", function(self)
		if IsShiftKeyDown()  then
			InterfaceOptionsFrame_OpenToCategory("FarmTracker")
		 else
			FarmTracker_MinimapHandler();
		 end
 end);
 FarmTracker_MinimapButton:SetScript("OnEnter", function(self)
         GameTooltip:SetOwner(self, "ANCHOR_LEFT");
         GameTooltip:SetText( "FarmTracker");
         GameTooltip:AddLine("Left-click to open farmTracker.");
         GameTooltip:AddLine("Right-click and drag to move this button.");
		 GameTooltip:AddLine("Shift-click to open FarmTracker Options.");
         GameTooltip:Show();
 end);
 FarmTracker_MinimapButton:SetScript("OnLeave", function(self)
         GameTooltip:Hide();
 end);
 
 FarmTracker_MinimapButton:SetScript("OnEvent", FarmTracker_MinimapButton_Init);
 
 
 
 
 --[[
 beginning work on the options page
 --]]
 
 local GAP = 8
 local tekcheck = LibStub("tekKonfig-Checkbox").new
 FarmTrackerOptions = {};
 FarmTrackerOptions.panel = CreateFrame( "Frame", "FarmTrackerPanel", UIParent );
 -- Register in the Interface Addon Options GUI
 -- Set the name for the Category for the Options Panel
 FarmTrackerOptions.panel.name = "FarmTracker";
 -- Add the panel to the Interface Options
 InterfaceOptions_AddCategory(FarmTrackerOptions.panel);
 
 local TKheading = LibStub("tekKonfig-Heading").new
 local title,subtitle = TKheading(FarmTrackerOptions.panel, "FarmTracker", "FarmTracker Options")
 local TKAboutPanel = LibStub("tekKonfig-AboutPanel").new("FarmTracker","FarmTracker")
 
 
 	local showMiniMap = tekcheck(FarmTrackerOptions.panel, nil, "Show MiniMap Icon", "TOPLEFT", subtitle, "BOTTOMLEFT", -2, -GAP)
	showMiniMap.tiptext = "Show the FarmTracker Minimap Icon"
	local checksound = showMiniMap:GetScript("OnClick")
	showMiniMap:SetChecked(FarmTracker_Save.ButtonShown)
	showMiniMap:SetScript("OnClick", function(self) checksound(self); FarmTracker_Save.ButtonShown  = not FarmTracker_Save.ButtonShown ; FarmTracker_MinimapButton_Init() end)
	
	
	local showGold = tekcheck(FarmTrackerOptions.panel, nil, "Show Gold", "TOPLEFT", showMiniMap, "BOTTOMLEFT", 0, -GAP)
	showGold.tiptext = "Show gold total on the FarmTracker Frame"
	
	local showFreeSlots = tekcheck(FarmTrackerOptions.panel, nil, "Show Free Bag Slots", "TOPLEFT", showGold, "BOTTOMLEFT", 0, -GAP)
	showFreeSlots .tiptext = "Show free bag slots on the FarmTracker Frame"

local f = CreateFrame("Frame")	
f:RegisterEvent("ADDON_LOADED")
function f:OnEvent(event)
	showMiniMap:SetChecked(FarmTracker_Save.ButtonShown)
end
f:SetScript("OnEvent", f.OnEvent);

 

 